<?php

namespace App\Providers;

use Core\ServiceProvider;
use MyLogger\Formatters\HtmlFormatter;
use MyLogger\Formatters\LineFormatter;
use MyLogger\Handlers\BrowserConsoleHandler;
use MyLogger\Handlers\RotatingFileHandler;
use MyLogger\Logger;

class LoggerServiceProvider extends ServiceProvider
{
    public function register()
    {
        $this->container->singleton('Logger', function ($container) {
            $app = $container->make('app');
            $logger = new Logger('app');
            // 添加滚动日志处理器 (保留5个日志文件)
            $rotatingHandler = new RotatingFileHandler($app->storagePath('log/app.log'), 5, Logger::DEBUG, new LineFormatter());
            $logger->addHandler($rotatingHandler);

            // 添加浏览器控制台处理器
            // $consoleHandler = new BrowserConsoleHandler(Logger::DEBUG, new HtmlFormatter());
            // $logger->addHandler($consoleHandler);
            return $logger;
        });
    }

    public function boot()
    {
    }
}
